cmake_minimum_required(VERSION 2.8)

message("-- Project: stdcrt")

#find_package(Clang REQUIRED)

file(GLOB stdcrt_src			"*.c")
file(GLOB stdcrt_code_src 		"code/*.c")
file(GLOB stdcrt_io_src 		"io/*.c")
file(GLOB stdcrt_msg_src 		"msg/*.c")
file(GLOB stdcrt_net_src 		"net/*.c")
file(GLOB stdcrt_stdlib_src 	"stdlib/*.c")
file(GLOB stdcrt_thread_src 	"thread/*.c")
file(GLOB stdcrt_string_src 	"string/*.c")
file(GLOB stdcrt_algorithm_src 	"algorithm/*.c")
file(GLOB stdcrt_init_src 		"init/*.c")
file(GLOB stdcrt_event_src 		"event/*.c")

add_library(stdcrt  STATIC 	${stdcrt_src}
							${stdcrt_code_src} 
							${stdcrt_io_src}
							${stdcrt_msg_src}
							${stdcrt_net_src} 
							${stdcrt_stdlib_src} 
							${stdcrt_thread_src}
							${stdcrt_string_src}
							${stdcrt_algorithm_src}
							${stdcrt_event_src}
							${stdcrt_init_src})

set_target_properties(stdcrt PROPERTIES OUTPUT_NAME "stdcrt")


include_directories(
		${PROJECT_SOURCE_DIR}/include
		${PROJECT_SOURCE_DIR}/crt)


if (${WIN_OS})

else()

	include(CheckTypeSize)
	include(CheckLibraryExists)
	include(CheckFunctionExists)

	check_type_size("uint8_t" EVENT__HAVE_UINT8_T)
	check_type_size("uint16_t" EVENT__HAVE_UINT16_T)
	check_type_size("uint32_t" EVENT__HAVE_UINT32_T)
	check_type_size("uint64_t" EVENT__HAVE_UINT64_T)
	check_type_size("short" EVENT__SIZEOF_SHORT BUILTIN_TYPES_ONLY)
	check_type_size("int" EVENT__SIZEOF_INT BUILTIN_TYPES_ONLY)
	check_type_size("unsigned" EVENT__SIZEOF_UNSIGNED BUILTIN_TYPES_ONLY)
	check_type_size("unsigned int" EVENT__SIZEOF_UNSIGNED_INT BUILTIN_TYPES_ONLY)
	check_type_size("long" EVENT__SIZEOF_LONG BUILTIN_TYPES_ONLY)
	check_type_size("long long" EVENT__SIZEOF_LONG_LONG BUILTIN_TYPES_ONLY)

	check_library_exists(rt clock_gettime "time.h" HAVE_CLOCK_GETTIME)
	check_library_exists(pthread sem_timedwait "semaphore.h" HAVE_SEM_TIME_WAIT)

	if (NOT HAVE_SEM_TIME_WAIT)
		message("-- stdcrt_sem_waittime: nofound")
	else()
		message("-- stdcrt_sem_waittime: found")
		add_definitions("-DHAVE_SYS_SEM_WAITTIME")
	endif()


	if (NOT HAVE_CLOCK_GETTIME)
		check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
		if (NOT HAVE_CLOCK_GETTIME)
			message("-- stdcrt_clock_gettime: nofound")
		else()
			message("-- stdcrt_clock_gettime: found")
			add_definitions("-DHAVE_SYS_CLOCK_GETTIME")
		endif()
	else()
		message("-- stdcrt_clock_gettime: found")
		add_definitions("-DHAVE_SYS_CLOCK_GETTIME")
		target_link_libraries(stdcrt rt)
	endif()


	include_directories(
		/usr/local/include
		/opt/local/include
		/usr/include)

	link_directories(/opt/local/lib)

	set_target_properties(stdcrt PROPERTIES COMPILE_FLAGS ${static})
	set_target_properties(stdcrt PROPERTIES COMPILE_FLAGS ${static_compile})

endif()


if (CMAKE_SYSTEM_NAME MATCHES "Android")
	target_link_libraries(stdcrt log)
	target_link_libraries(stdcrt dl)
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
	target_link_libraries(stdcrt dl pthread)
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
    target_link_libraries(stdcrt pthread dl)
elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
    target_link_libraries(stdcrt pthread)
elseif (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
    target_link_libraries(stdcrt pthread)
elseif (CMAKE_SYSTEM_NAME MATCHES "CYGWIN")
    target_link_libraries(stdcrt pthread dl)
elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
	
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
     target_link_libraries(stdcrt pthread dl)
else()
	message(FATAL_ERROR "unknown CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}")
endif()

















